<?php

/**
 * This is the model class for table "edu_article".
 *
 * The followings are the available columns in table 'edu_article':
 * @property integer $id
 * @property string $title
 * @property string $content
 * @property string $tag
 * @property integer $categoryid
 * @property integer $createtime
 * @property integer $updatetime
 * @property integer $parentid
 * @property integer $userid
 * @property integer $views
 * @property integer $comments
 */
class Article extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'edu_article';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title,summary', 'required'),
			array('categoryid, createtime, updatetime, parentid, userid, views, comments, status', 'numerical', 'integerOnly'=>true),
			array('title, tag', 'length', 'max'=>150),
			array('content', 'safe'),
			array('status','in','range'=>array(200,300,400,500)),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('title,tag, categoryid, createtime, parentid,status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user'=>array(self::BELONGS_TO,"User","userid"),
			'navigation'=>array(self::BELONGS_TO,'Navigation','parentid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => '标题',
			'summary'=>'简介',
			'content' => '内容',
			'tag' => '标签',
			'categoryid' => '分类',
			'createtime' => '发布时间',
			'updatetime' => '修改时间',
			'parentid' => '栏目',
			'userid' => '用户id',
			'views' => '查看数',
			'comments' => '回复数',
			'status'=>'状态',  //状态  200 发布 300 草稿 400审核中 500 审核通过 600 审核不通过
			'pic'=>'图片'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('title',$this->title,true);
		$criteria->compare('tag',$this->tag,true);
		$criteria->compare('categoryid',$this->categoryid);
		$criteria->compare('createtime',$this->createtime);
		$criteria->compare('updatetime',$this->updatetime);
		$criteria->compare('parentid',$this->parentid);
		$criteria->compare('status', $this->status);
		$criteria->order='createtime desc,updatetime desc';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>Yii::app()->params["pagesize"]
				)
		));
	}

	public function beforeSave()
	{
		if($this->isNewRecord)
		{
			$this->userid= Yii::app()->session['user']->userid;
			$this->createtime = time();
		}
		else
		{
			$this->updatetime = time();
		}
		return parent::beforeSave();
	}

	public function loadStatus()
	{
		return array(
					200=>'发布',
					300=>'草稿',
					400=>'审核中',
					500=>'审核通过',
					600=>'审核不通过'
				);
	}


	public  function Page()
	{
		$criteria=new CDbCriteria;
		$criteria->select= 'id,title,IFNULL(summary,content)  summary,createtime,views,comments,pic';
		$criteria->compare('status', 200);
		$criteria->order='createtime  desc';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>Yii::app()->params['article_pagesize']
				)
		));
	}
}